#!/bin/bash

function usage(){
        echo "NAME:"
        echo "  "$0
        echo ""
        echo "USAGE:"
        echo "  "$0" [command options] [arguments...]"
        echo ""
        echo "VERSION:"
        echo "  v0.0.1"
        echo ""
        echo "OPTIONS::"
        echo "  -help      print this help text:"
        echo "  -m    install|init|start|stop|rm"
        echo "  -i     127.0.0.1|192.168.x.x  default value is 127.0.0.1"
}

function install_docker_container(){
        echo "apt update......"
        apt update

        #安装 docker
        echo "检查Docker......"
        docker -v
        if [ $? -eq  0 ]; then
                echo "检查到Docker已安装!"
        else
                echo "安装docker环境..."
                curl -sSL https://get.docker.com/ | sh
                echo "安装docker环境...安装完成!"
        fi
        
        
        echo "启动 docker"
        service docker start
        
        echo "拉取image"
        docker pull openstf/stf:latest
        docker pull rethinkdb:2.3
        docker pull nginx:1.7.10
        docker pull sorccu/adb:latest
        
        #echo "创建文件夹"
        if [ ! -d rethinkdb_data ];then
                mkdir rethinkdb_data
        fi
        if [ ! -d storage ];then
                mkdir storage
                chmod 777 storage
        fi
}

function init_container(){
        echo "新创建容器，并启动"
        echo "启动nginx"
        docker run -d  --name nginx -v "${WORKDIR}/nginx.conf:/etc/nginx/nginx.conf:ro" --net host nginx:1.7.10 nginx
        
        echo "启动 rethinkdb"
        docker run -d --name some-rethink -v "${WORKDIR}/rethinkdb_data:/data" --net host rethinkdb:2.3 rethinkdb --cache-size 2048 --no-update-check
        sleep 3
        
        # 初始化数据表,只需要执行一次
        echo "rethinkdb init"
        docker run --rm --name stf-migrate --net host openstf/stf:latest stf migrate
        sleep 3
        
        echo "启动 stf app"
        docker run -d --name stf-app --net host -e "SECRET=YOUR_SESSION_SECRET_HERE" openstf/stf:latest stf app --port 7100 --auth-url http://${HOSTNAME}/auth/mock/ --websocket-url ws://${HOSTNAME}/
        sleep 3
        
        echo "启动 stf auth-mock"
        docker run -d --name stf-auth --net host -e "SECRET=YOUR_SESSION_SECRET_HERE" openstf/stf:latest stf auth-mock --port 7101 --app-url http://${HOSTNAME}/
        sleep 1
        
        echo "启动 stf websocket"
        docker run -d --name websocket --net host -e "SECRET=YOUR_SESSION_SECRET_HERE" openstf/stf:latest stf websocket --port 7102 --storage-url http://${HOSTNAME}/ --connect-sub tcp://127.0.0.1:7150 --connect-push tcp://127.0.0.1:7170
        sleep 1
        
        echo "启动 stf api"
        docker run -d --name stf-api --net host -e "SECRET=YOUR_SESSION_SECRET_HERE" openstf/stf:latest stf api --port 7103 --connect-sub tcp://127.0.0.1:7150 --connect-push tcp://127.0.0.1:7170 --connect-sub-dev tcp://127.0.0.1:7250 --connect-push-dev tcp://127.0.0.1:7270
        sleep 1
        
        echo "启动 stf storage-plugin-apk"
        docker run -d --name storage-apk --net host openstf/stf:latest stf storage-plugin-apk --port 7104 --storage-url http://${HOSTNAME}/
        sleep 1
        
        echo "启动 stf storage-plugin-image"
        docker run -d --name storage-image --net host openstf/stf:latest stf storage-plugin-image --port 7105 --storage-url http://${HOSTNAME}/
        sleep 1
        
        echo "启动 stf storage-temp"
        docker run -d --name storage-temp --net host -v "${WORKDIR}/storage:/data" openstf/stf:latest stf storage-temp --port 7106 --save-dir /data
        sleep 1
        
        echo "启动 stf triproxy app"
        docker run -d --name triproxy-app --net host openstf/stf:latest stf triproxy app --bind-pub "tcp://*:7150" --bind-dealer "tcp://*:7160" --bind-pull "tcp://*:7170"
        sleep 1
        
        echo "启动 stf processor"
        docker run -d --name stf-processer --net host openstf/stf:latest stf processor stf-processer --connect-app-dealer tcp://127.0.0.1:7160 --connect-dev-dealer tcp://127.0.0.1:7260
        sleep 1
        
        echo "启动 stf triproxy dev"
        docker run -d --name triproxy-dev --net host openstf/stf:latest stf triproxy dev --bind-pub "tcp://*:7250" --bind-dealer "tcp://*:7260" --bind-pull "tcp://*:7270"
        sleep 1
        
        echo "启动 stf reaper dev"
        docker run -d --name stf-reaper --net host openstf/stf:latest stf reaper dev --connect-push tcp://127.0.0.1:7270 --connect-sub tcp://127.0.0.1:7150 --heartbeat-timeout 30000



        echo "启动adbd"
        docker run -d --name adbd --privileged -v /dev/bus/usb:/dev/bus/usb --net host sorccu/adb:latest
        sleep 3
        
        echo "启动stf provider"
        docker run -d --name provider1 --net host openstf/stf \
        stf provider --name provider1 \
                --connect-sub tcp://${HOSTNAME}:7250 \
                --connect-push tcp://${HOSTNAME}:7270 \
                --min-port=15000 --max-port=25000 --heartbeat-interval 20000 --allow-remote --no-cleanup \
                --storage-url http://${HOSTNAME}
}

function detect_container(){
        ret=0
        if [[ -n $(docker ps | grep $1) ]];then
                #echo $1" is running"
                ret=0
        else
                #echo $1" is not running"
                ret=1
        fi

        return $ret
}

# stf容器列表
C_TBL=("stf-reaper" \
        "triproxy-dev" \
        "stf-processer" \
        "triproxy-app" \
        "storage-temp" \
        "storage-image" \
        "storage-apk" \
        "stf-api" \
        "websocket" \
        "stf-auth" \
        "stf-app" \
        "some-rethink" \
        "nginx" \
        "provider1" \
        "adbd")


function start_container(){
        echo "启动现有容器"

        for i in "${!C_TBL[@]}"
        do
                docker start ${C_TBL[i]}
        done
        sleep 3

        # 由于启动顺序不当导致容器启动失败的，再启动一次
        for i in "${!C_TBL[@]}"
        do
                r=$(detect_container ${C_TBL[i]})
                ret=`echo $?`
                if [ $ret -eq 1 ];then
                        #echo "container "${C_TBL[i]}" start failed first, start second now"
                        docker start ${C_TBL[i]}
                        sleep 1
                fi
        done
}

function stop_container(){
        echo "停止现有容器"

        for i in "${!C_TBL[@]}"
        do
                docker stop ${C_TBL[i]}
        done
}

function rm_container(){
        echo "删除现有容器"
        for i in "${!C_TBL[@]}"
        do
                docker rm ${C_TBL[i]}
        done
}

if [ ! -n "$1" ] ;then
        usage
        exit -1
fi

#获取当前目录
WORKDIR=$(cd $(dirname $0); pwd)
MODE=""
HOSTNAME="127.0.0.1"
while getopts "h:m:i:" opt
do
        case $opt in
                h)
                       usage
                       ;;
                m)
                        #echo "mode option $OPTARG"
                        MODE=$OPTARG
                        ;;
                i)
                        #echo "ip option $OPTARG"
                        if [ "$OPTARG" != "" ];then
                                HOSTNAME=$OPTARG
                        fi
                        ;;
                ?)
                        usage
                        exit -1
                        ;;
        esac
done
echo "服务IP:${HOSTNAME}"

case $MODE in
        "install")
                echo "install command"
                install_docker_container
                ;;
        "init")
                echo "create and launch container"
                init_container
                ;;
        "start")
                echo "start command"
                start_container
                ;;
        "stop")
                echo "stop command"
                stop_container
                ;;
        "rm")
                echo "rm command"
                rm_container
                ;;
        *)
                echo "invalid options!"
                echo ""
                usage
                exit -1
                ;;
esac


